package com.fqgj.sentry.policy.server;

import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.sentry.common.exception.BizException;
import com.fqgj.sentry.common.exception.enums.ErrorCodeEnums;
import com.fqgj.sentry.common.utils.ApiPage;
import com.fqgj.sentry.policy.client.EventService;
import com.fqgj.sentry.policy.dao.PcColumnDao;
import com.fqgj.sentry.policy.dao.PcEventDao;
import com.fqgj.sentry.policy.dao.PcPolicySetsDao;
import com.fqgj.sentry.policy.domain.Event;
import com.fqgj.sentry.policy.entity.PcEventEntity;
import com.fqgj.sentry.policy.enums.ColumnTypeEnum;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * Created by zwh @yuntu-inc.com
 *
 * @description
 * @create 2017/7/27 0027 上午 10:21
 */
@Service
public class EventServiceImpl implements EventService {

    @Autowired
    private PcEventDao pcEventDao;
    @Autowired
    private PcColumnDao pcColumnDao;
    @Autowired
    private PcPolicySetsDao pcPolicySetsDao;

    @Override
    public PcEventEntity saveEvent(PcEventEntity pcEventEntity) {
        //事件code  不能为空
        if (StringUtils.isEmpty(pcEventEntity.getCode())) {
            throw new BizException(ErrorCodeEnums.event_code_is_not_error);
        }
        //该商户下  事件code 不能重复
        PcEventEntity eventEntity = pcEventDao.selectByMerchantIdAndCode(pcEventEntity.getMerchantId(), pcEventEntity.getCode());
        if (null != eventEntity) {
            throw new BizException(ErrorCodeEnums.event_code_is_exist_error);
        }
        return pcEventDao.insert(pcEventEntity);
    }

    @Override
    public Integer deleteEvent(Long eventId) {
        selectEvent(eventId);
        if (CollectionUtils.isNotEmpty(pcPolicySetsDao.selectByEventId(eventId))) {
            throw new BizException(ErrorCodeEnums.policy_set_event_is_exit, "该事件已被策略集绑定");
        }
        //该商户有没有没策略集或字段关联事件
        if (CollectionUtils.isNotEmpty(pcColumnDao.selectByEventId(eventId, ColumnTypeEnum.field_function.getType()))) {
            throw new BizException(ErrorCodeEnums.column_has_event, "该事件已被字段或函数绑定");
        }

        return pcEventDao.deleteByPrimaryKey(eventId);
    }

    @Override
    public Integer updateEvent(Event event) {
        //查询
        PcEventEntity pcEventEntity = selectEvent(event.getId())
                .setName(event.getName())
                .setInfo(event.getInfo());
        return pcEventDao.updateByPrimaryKey(pcEventEntity);
    }

    @Override
    public List<PcEventEntity> listEvent(Long merchantId, String code, String name, ApiPage apiPage) {
        Page<PcEventEntity> page = PageHelper.startPage(apiPage.getPageNum(), apiPage.getPageSize());
        List<PcEventEntity> eventEntityList = pcEventDao.selectEventList(merchantId, code, name);
        apiPage.setTotal(page.getTotal());
        return eventEntityList;
    }

    /**
     * 根据 商户id 查询事件集合
     *
     * @param merchantId
     * @return
     */
    @Override
    public List<PcEventEntity> comboEvent(Long merchantId) {
        return pcEventDao.selectByMerchantId(merchantId);
    }

    public PcEventEntity selectEvent(Long id) {
        PcEventEntity pcEventEntity = pcEventDao.selectByPrimaryKey(id);
        if (null == pcEventEntity) {
            throw new BizException(ErrorCodeEnums.event_is_not_exist_error);
        }
        return pcEventEntity;
    }

    @Override
    public PcEventEntity selectByMerchantIdAndCode(Long merchantId, String code) {
        return pcEventDao.selectByMerchantIdAndCode(merchantId, code);
    }
}
